<template>
	<view class="content">
		<!-- Banner -->
		<view class="topbanner">
			<swiper class="topbanner-swiper" :indicator-dots="showBannerDots" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item  v-for="(item,index) in banners" :key="index" @click="bannerClick(item)">
					<view class="swiper-item">
						<image class="swiper-item-image" :src="item.icon" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 中间的分类 -->
		<view class="category_grid">
			<view class="category_grid_left" @click="jumpToMingXiangPage()">
				<image class="category_grid_left_image" src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-83418c8b-2862-4eac-a6ec-bf64544c5621/e6a399e8-774e-4662-a23c-20d69f0cc003.jpg" mode="aspectFill"></image>
			</view>
			<view class="category_grid_right">
				<view class="category_grid_right_top" @click="jumpToListenStoryPage()">
					<image class="category_grid_right_top_image" src="../../static/home/grid_story.png" mode="aspectFill"></image>
					<view class="category_grid_right_top_text">听故事</view>
				</view>
				<view class="category_grid_right_bottom" @click="jumpToNoonRestPage()">
					<image class="category_grid_right_bottom_image" src="../../static/home/grid_xiaoqi.png" mode="aspectFill"></image>
					<view class="category_grid_right_bottom_text">中午小憩</view>
				</view>
			</view>
		</view>
		
		<!-- tabs 分类 -->
		<v-tabs v-model="currentSelectedTab" :tabs="tabs" :scroll="false" @change="changeTab" bgColor=$zm-main-color color="#C4C4C4" activeColor="#FFFFFF" lineColor="#FF4040"></v-tabs>
		<view class="music_list">
			<view class="music_item" v-for="(item,index) in musicList" :key="index" @click="chooseVideoItem(index)">
				<image :src=item.icon mode="aspectFill"></image>
				<view class="music_title"> {{ item.name }} </view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				/* 当前选择的tab */
				currentSelectedTab: 0,
				/* tabs分类列表 */
				tabs: [],
				/* 分类的音频数据列表 */
				musicList:[],
				/* 后端请求的音频数据列表 */
				allMusicArray:[],
				/* 后端请求的Banner列表 */
				banners: [],
				/* 是否显示Banner上的指示器 */
				showBannerDots: false,
			};
		},
		onLoad() {
			console.log("页面加载了");
			this.tabs = getApp().globalData.homeMusicTypes;
			this.requestMusicData();
			this.requestHomeBannerList();
		},
		onPullDownRefresh() {
			this.requestMusicData();
		},
		onShareAppMessage() {
			return {
				title: "享受来自大自然的健康睡眠",
				path: "/pages/appLaunch/appLaunch",
				imageUrl: "/static/share/share_icon.jpg"
			}
		},
		methods: {
			/* 请求后端的数据 */
			requestMusicData() {
				uni.showLoading({
					title:"数据加载中"
				});
				var that = this;
				this.$api.get_music_list().then((res)=>{
					uni.hideLoading()
					uni.stopPullDownRefresh();
					that.allMusicArray = res.data;
					// 保存数据到全局变量，小憩页面用到了
					getApp().globalData.homeAllMusics = that.allMusicArray;
					// 默认选择第0个
					that.changeTab(0);
				}).catch((err)=>{
					uni.hideLoading()
					uni.stopPullDownRefresh();
					uni.showModal({
					    title: '提示',
						content: '数据加载失败',
						confirmText: '重试',
						confirmColor: '#FF4040',
					    success: function (res) {
					        if (res.confirm) {
								that.requestMusicData();
					        }
					    }
					});
				})
			},
			/* 获取首页的Banner列表 */
			requestHomeBannerList() {
				var that = this;
				this.$api.requestHomeBannerList().then((res)=>{
					that.banners = res.data;
					that.showBannerDots = that.banners.length > 1 ? true : false; 
				}).catch((err)=>{
					console.log(err);
				});
			},
			/* 点击了Banner */
			bannerClick(item) {
				console.log(item);
				if (item.type == "h5") {
					uni.navigateTo({
						url:"../jmWebview/jmWebview?jmurl=" + item.weburl
					});
				}else if (item.type == "nav") {
					uni.navigateTo({
						url: item.navpage
					});
				}else {}
			},
			/* 切换选中的tab */
			changeTab(index) {
				this.currentSelectedTab = index;
				let type = this.tabs[index].type;
				this.musicList = this.allMusicArray.filter((item)=>{
					return item.type == type
				});
			},
			
			/* 选择某一个cell */
			chooseVideoItem(index) {
				var that = this;
				var playerArray = this.musicList.map((item)=>{
					var newItem = new Object();
					newItem.title = item.name;
					newItem.music = item.music;
					newItem.icon = item.icon;
					newItem.type = that.getTabsNameWithType(item.type);
					newItem.from = "home";
					return newItem;
				});
				var itemsString = JSON.stringify(playerArray);
				uni.navigateTo({
					url:"../playerModel/musicplayer/musicplayer?items=" + itemsString + "&index=" + index
				});
			},
			/* 根据tabs的type获取tab的name */
			getTabsNameWithType(type) {
				var itemsArray = this.tabs.filter((tab)=>{
					return tab.type == type;
				});
				return itemsArray[0].name;
			},
			
			/* 跳转到冥想页面 */
			jumpToMingXiangPage() {
				uni.navigateTo({
					url:"../meditationModel/meditation/meditation"
				});
			},
			
			/* 跳转到听故事页面 */
			jumpToListenStoryPage() {
				uni.switchTab({
					url:"../storylist/storylist"
				});
			},
			
			/* 跳转到中午小憩 */
			jumpToNoonRestPage() {
				console.log("跳转到中午小憩");
				uni.navigateTo({
					url:"../nap/resetnap/resetnap"
				});
			},
			
		}
	}
</script>

<style lang="scss">
	
	/* top banner */
	.topbanner {
		margin: 25rpx;
		width: 700rpx;
		height: 298rpx;
	}
	.topbanner-swiper {
		width: 100%;
		height: 100%;
	}
	.swiper-item {
		width: 100%;
		height: 100%;
	}
	
	.swiper-item-image {
		width: 100%;
		height: 100%;
	}
	
	/* 中间的分组 */
	.category_grid {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		margin: 25rpx;
	}
	
	.category_grid_left {
		width: 340rpx;
		height: 220rpx;
	}
	
	.category_grid_left_image {
		width: 100%;
		height: 100%;
	}
	
	.category_grid_right {
		width: 340rpx;
		height: 220rpx;
	}
	
	.category_grid_right_top {
		background-color: #333333;
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		background-image: url("https://vkceyugu.cdn.bspapp.com/VKCEYUGU-83418c8b-2862-4eac-a6ec-bf64544c5621/d872c3b0-9fc3-4af7-b49c-c971acc274c5.jpeg");
	}
	
	.category_grid_right_top_image {
		width: 80rpx;
		height: 80rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
	}
	
	.category_grid_right_top_text {
		color: #FFFFFF;
		font-size: 40rpx;
	}
	
	.category_grid_right_bottom {
		background-color: #333333;
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		background-image: url("https://vkceyugu.cdn.bspapp.com/VKCEYUGU-83418c8b-2862-4eac-a6ec-bf64544c5621/c2bf25e8-2e4a-4316-ad17-3c16eed864b4.jpeg");
	}
	
	.category_grid_right_bottom_image {
		width: 80rpx;
		height: 80rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
	}
	
	.category_grid_right_bottom_text {
		color: #FFFFFF;
		font-size: 40rpx;
	}

	/* 下面的音频分类 */
	.music_list {
		margin: 25rpx 0;
		display: flex;
		flex-wrap: wrap;
		.music_item {
			width: 187.5rpx;
			height: 187.5rpx;
			margin-top: 15rpx;
			text-align: center;
			image {
				margin-top: 15rpx;
				width: 60rpx;
				height: 60rpx;
			}
			.music_title {
				margin-top: 10rpx;
				font-size: 30rpx;
				color: #C4C4C4;
			}
		}
	}

</style>
